package com.beiding.my.query.follow;

import com.beiding.my.query.Expression;
import com.beiding.my.query.Follow;
import com.beiding.my.query.ParamHolder;
import com.google.common.base.Joiner;

import java.util.ArrayList;
import java.util.List;

/**
 * 有体连接
 */
public class BodyConnector implements Follow {

    private Expression[] body;
    private String symbol;

    public BodyConnector(String symbol, Expression[] body) {
        this.symbol = symbol;
        this.body = body;
    }

    @Override
    public String create(ParamHolder paramHolder) {

        if (body.length > 0) {
            if (body.length == 1) {
                return body[0].create(paramHolder);
            } else {
                List<String> cs = new ArrayList<>();
                for (Expression expression : body) {
                    cs.add(expression.create(paramHolder));
                }
                return "(" + Joiner.on(symbol).join(cs) + ")";
            }
        } else {
            return "";
        }
    }
}
